// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var footer = require("../footer/footer_main");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var tools = require("tools");
	var css_dialog = require("css_dialog");
	var databind = require("databind");
	var html_addOtherStatement = require("./html_addOtherStatement.html");
	var storage = window.sessionStorage;
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var oData = {
		pageIndex: pageIndex,
		pageSize: pageSize,
		totalPageCount: totalPageCount,
		totalRecordCount: totalRecordCount
	};
	var init = function() {
		footer.init();
		$("#footer").removeClass("none");
		leftMenu.init(5);
		topMenu.init(1);
		router.init();
		$(".table-section").removeClass("none");
		$(".footer").removeClass("none");
		tools.loadList("inquireFinanceOtherMoneyStatementPageList.json", $("#receiptManageBody"), $("#receiptManageTpl"), oData, function() {
			$(".detail").unbind("click").click(function() {
				//router.route("insuranceManage/insuranceDetail.html?insuranceId=" + $(this).parents("tr").attr("insuranceId") + "&financeId=" + $(this).parents("tr").attr("financeId"));
			});
		});
		$(".otherStatementManage").parent().parent().parent().addClass("leftMenu-open");
		$(".otherStatementManage").parent().addClass("leftMenu-active");
		initEvent();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			location.href = "../login/login.html";
		}
		
		$(".addOtherStatement").unbind("click").click(function(){
			var ii = layer.open({
			  title: '添加其他应收应付',
			  type: 1,
			  skin: 'layui-layer-rim', //加上边框
			  area: ['800px', '300px'], //宽高
			  content: html_addOtherStatement,
			  btn: ['确定', '取消'],
			  yes: function(){
			  	databind.submitDom($(".createFinanceOtherMoneyStatement"), function(){
			  		layer.msg("添加成功");
			  		layer.close(ii);
			  		location.href = "../moneyStatementManage/otherStatementManage.html"
			  	});
			  }
			});
			
//			tools.clearNoNum($(".createFinanceOtherMoneyStatement input[name=amount]"));
			
			$(".createFinanceOtherMoneyStatement select[name=disbursementType]").unbind("change").change(function(){
				if ($(this).val() == "1") {
					$(".createFinanceOtherMoneyStatement select[name=disbursementId]").html("<option value=''>平台</option>");
					$(".createFinanceOtherMoneyStatement input[name=disbursementName]").val("平台");
				} else if ($(this).val() == "2") {
					$(".createFinanceOtherMoneyStatement select[name=disbursementId]").html("");
					tools.loadSelectList($(".createFinanceOtherMoneyStatement select[name=disbursementId]"), "inquireEnterpriseList.json", "id", "name", null);
				}
			});
			
			$(".createFinanceOtherMoneyStatement select[name=receiptType]").unbind("change").change(function(){
				if ($(this).val() == "1") {
					$(".createFinanceOtherMoneyStatement select[name=receiptId]").html("<option value=''>平台</option>");
					$(".createFinanceOtherMoneyStatement input[name=receiptName]").val("平台");
				} else if ($(this).val() == "2") {
					$(".createFinanceOtherMoneyStatement select[name=receiptId]").html("");
					tools.loadSelectList($(".createFinanceOtherMoneyStatement select[name=receiptId]"), "inquireEnterpriseList.json", "id", "name", null);
				}
			});
			
			$(".createFinanceOtherMoneyStatement select[name=disbursementId]").unbind("change").change(function(){
				$(".createFinanceOtherMoneyStatement input[name=disbursementName]").val($(this).find("option:selected").text());
			});
			
			$(".createFinanceOtherMoneyStatement select[name=receiptName]").unbind("change").change(function(){
				$(".createFinanceOtherMoneyStatement input[name=receiptName]").val($(this).find("option:selected").text());
			});
		});
		
		$("#main").on("click", ".confirm", function(){
			var $this = $(this);
			layer.confirm('是否确认通过该条应收应付，确认后将会生成一条流水？', {
			  btn: ['确定','取消'] //按钮
			}, function(){
			  confirmFinanceOtherMoneyStatement($this.parents("tr").attr("otherMoneyStatementId"));
			}, function(){
			  layer.closeAll();
			});
		});

	};
	
	//收款确认
	var confirmFinanceOtherMoneyStatement = function(otherMoneyStatementId) {
		base.fnAjax({
			action: "confirmFinanceOtherMoneyStatement.json", //接口名称
			oData: {
				otherMoneyStatementId: otherMoneyStatementId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					layer.msg("确认成功！");
					layer.closeAll();
					location.href = "../moneyStatementManage/otherStatementManage.html";
				}
			}
		});
	};

	return {
		init: init
	};
});